<!DOCTYPE html>

<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title>登录</title>

  <link href="css/min.css" rel="stylesheet">

</head>

<body  class="bg-gradient-primary">

  <div class="container" id="app">

    <!-- Outer Row -->
    <div class="row justify-content-center">

      <div class="col-xl-10 col-lg-12 col-md-9">

        <div class="card o-hidden border-0 shadow-lg my-5">
          <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
              <div class="col-lg-6 d-none d-lg-block bg-login-image"></div>
              <div class="col-lg-6">
                <div class="p-5">
                  <div class="text-center">
                    <h1 class="h4 text-gray-900 mb-4">欢迎回来!</h1>
                  </div>

                    <div class="form-group">
                      <input type="text" class="form-control form-control-user" name="usrname" v-model="userinfo.name" id="user" placeholder="请输入账号">
                    </div>
                    <div class="form-group">
                      <input type="password" class="form-control form-control-user" name="pwd" v-model="userinfo.pass" id="pwd" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                      <div class="custom-control custom-checkbox small">
                        <input type="checkbox" v-model="radio" class="custom-control-input" id="customCheck">
                          <label class="custom-control-label" for="customCheck">Remember Me</label>
                      </div>
                    </div>

                    <font color="red"></font>
                     <button type="button" class="btn btn-primary btn-user btn-block"  @click="login">登录</button>
                    <hr>

                  <hr>
                  <div class="text-center">
                    <a class="small" href="regist.html">注册账号!</a>       </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body></html>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
<script>

  var app = new Vue({
    el:"#app",
    data:{
      userinfo:{},
      message:'',
      radio:''
    },
    created:function (){
      if(localStorage.getItem("userinfo")!=null){
        var user=JSON.parse(localStorage.getItem("userinfo"));
        axios.post("http://localhost:8080/user/login",user)
                .then(res=>{
                  var token= res.data.data.token;
                  this.message="";
//登录成功保存token
                  window.localStorage.setItem("token",token);
//从哪个页面跳到的登录，则登录成功后回到哪个页面
                  var search = window.location.search;//?url=.....
                  if(false){

                    var url = search.slice(5);
                    window.location.href=url;
                  }
                  else{
//默认回到首页

                    alert("登录成功")
                    location.href="index.html"
                  }
                })
                .catch(e=>{
                  this.message="登录失败，请重新登录！12344";
                  alert(this.message)
                });

      }
    },
    methods:{
      login:function(){


        axios.post("http://localhost:8080/user/login",this.userinfo)
                .then(res=>{
                  var token= res.data.data.token;
                  this.message="";
//登录成功保存token
                  window.localStorage.setItem("token",token);
//从哪个页面跳到的登录，则登录成功后回到哪个页面
                  var search = window.location.search;//?url=.....
                  if(false){

                    var url = search.slice(5);
                    window.location.href=url;
                  }
                  else{
//默认回到首页
                    if(this.radio){
                      alert("12355")
                      let log=JSON.stringify(this.userinfo)
                      localStorage.setItem("userinfo",log)
                    }
                    alert("登录成功")
                    location.href="index.html"
                  }
                })
                .catch(e=>{
                  this.message="登录失败，请重新登录！";
                  alert(this.message)
                });
      }
    }
  });
</script>